package cn.est.service.impl;
import cn.est.mapper.SmsLogMapper;
import cn.est.pojo.SmsLog;
import cn.est.service.SmsLogService;
import javax.annotation.Resource;

import cn.est.utils.MathUtils;
import cn.est.utils.redis.RedisKeyUtils;
import cn.est.utils.redis.RedisUtils;
import cn.est.utils.sms.SendSms;
import com.mysql.cj.log.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import cn.est.dto.Page;
import cn.est.constants.Constants;
import cn.est.utils.EmptyUtils;
import org.springframework.transaction.annotation.Transactional;

/*
短信功能业务层实现
 */
@Service
@Transactional
public class SmsLogServiceImpl implements SmsLogService {

    private Logger logger= LoggerFactory.getLogger(SmsLogServiceImpl.class);

    @Resource
    private SmsLogMapper smsLogMapper;

    @Resource
    private SendSms sendSms;

    @Resource
    private RedisUtils redisUtils;

    /*
    异步发送短信
     */
    @Override
    public void sendMessage(String phone, Integer codeType, Long uId) {
        //短信发送状态
        Integer status=Constants.Sms.STATUS_SEND_FAILED;
        //调用短信发送接口，发送短信
        String sendCode= MathUtils.random();
        String msgResult=sendSms.send(phone,sendCode);
        logger.info("发送短信phone:{}，验证码:{}",phone,sendCode);
        //2.保存短信发送信息
        if(msgResult!=null && msgResult.equals("OK")){
            status=Constants.Sms.STATUS_SEND_SUCCESS;
            //3.缓存短信验证码，并保存有效性
            String key = RedisKeyUtils.formatKeyWithPrefix(Constants.Redis.PREFIX_SMS, codeType.toString(), phone);
            //把验证码放在redis中
            redisUtils.putValue(key,sendCode,Constants.Duration.MINUTE_INT);
        }
        SmsLog smsLog = new SmsLog();
        smsLog.setPhone(phone);
        smsLog.setCodeType(codeType);
        smsLog.setMessage(sendCode);
        smsLog.setFailInfo(msgResult);
        smsLog.setStatus(status);
        smsLog.setCreatedTime(new Date());
        smsLog.setCreatedUserId(uId);
        smsLog.setIsDelete(0);
        smsLogMapper.insertSmsLog(smsLog);
    }

    public SmsLog getSmsLogById(Long id){
        return smsLogMapper.getSmsLogById(id);
    }

    public List<SmsLog>	getSmsLogListByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogListByMap(param);
    }

    public Integer getSmsLogCountByMap(Map<String,Object> param){
        return smsLogMapper.getSmsLogCountByMap(param);
    }

    public Integer qdtxAddSmsLog(SmsLog smsLog){
            smsLog.setCreatedTime(new Date());
            return smsLogMapper.insertSmsLog(smsLog);
    }

    public Integer qdtxModifySmsLog(SmsLog smsLog){
        smsLog.setUpdatedTime(new Date());
        return smsLogMapper.updateSmsLog(smsLog);
    }

    public Integer qdtxDeleteSmsLogById(Long id){
        return smsLogMapper.deleteSmsLogById(id);
    }

    public Integer qdtxBatchDeleteSmsLog(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return smsLogMapper.batchDeleteSmsLog(param);
    }

    public Page<SmsLog> querySmsLogPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = smsLogMapper.getSmsLogCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Page.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Page.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SmsLog> smsLogList = smsLogMapper.getSmsLogListByMap(param);
        page.setList(smsLogList);
        return page;
     }


}
